package com.shangcheng.server.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangcheng.model.domain.OmsOrder;
import com.shangcheng.model.domain.OmsOrderItem;
import com.shangcheng.model.domain.OmsOrderOperateHistory;
import com.shangcheng.model.dto.OmsOrderDto;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.server.mapper.OmsOrderItemMapper;
import com.shangcheng.server.mapper.OmsOrderMapper;
import com.shangcheng.server.mapper.OmsOrderOperateHistoryMapper;
import com.shangcheng.server.service.OmsOrderService;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author JavaClimber
 * @version 1.0
 * @date 2022/5/13 0013 zihao
 */
@Service
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements OmsOrderService {
    @Autowired
    private OmsOrderOperateHistoryMapper omsOrderOperateHistoryMapper;
    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;
    
    @Override
    public PageResult lists(Map<String, String> omsOrder, Integer pageNum, Integer pageSize) {
        //先从oms获取时间,然后用糊涂工具包转换成date
        String createTime = omsOrder.get("createTime");
        //自定义一个时间范围,后转成date
        Date beginOfDay = new Date();
        Date endOfDay = new Date();
        if (StringUtils.isNotEmpty(createTime)) {
            Date date = DateUtil.parse(createTime);
            beginOfDay = DateUtil.beginOfDay(date);
            endOfDay = DateUtil.endOfDay(date);
        }
        //提取订单编号(模糊)
        String orderSn = omsOrder.get("orderSn");
        //提取订单类型：0->正常订单；1->秒杀订单
        String orderType = omsOrder.get("orderType");
        //提取收货人姓名或者号码(模糊)
        String receiverKeyword = omsOrder.get("receiverKeyword");
        //提取订单来源：0->PC订单；1->app订单
        String sourceType = omsOrder.get("sourceType");
        //提取订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
        String status = omsOrder.get("status");
        
        Page<OmsOrder> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OmsOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询在时间范围之间
        lambdaQueryWrapper.between(StringUtils.isNotEmpty(createTime), OmsOrder::getCreateTime, beginOfDay, endOfDay)
                //提取订单编号
                .like(StringUtils.isNotEmpty(orderSn), OmsOrder::getOrderSn, orderSn)
                //查询订单类型
                .eq(StringUtils.isNotEmpty(orderType), OmsOrder::getOrderType, orderType)
                //提取收货人姓名或者号码
                .like(StringUtils.isNotEmpty(receiverKeyword), OmsOrder::getReceiverName, receiverKeyword)
                .or(StringUtils.isNotEmpty(receiverKeyword)).like(StringUtils.isNotEmpty(receiverKeyword), OmsOrder::getReceiverPhone, receiverKeyword)
                //提取订单来源：0->PC订单；1->app订单
                .eq(StringUtils.isNotEmpty(sourceType), OmsOrder::getSourceType, sourceType)
                //提取订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
                .eq(StringUtils.isNotEmpty(status), OmsOrder::getStatus, status);
        
        //封装page
        Page<OmsOrder> pp = this.page(page, lambdaQueryWrapper);
        PageResult pageResult = new PageResult(Convert.toInt(pp.getCurrent()), Convert.toInt(pp.getSize()), Convert.toInt(pp.getTotal()), pp.getRecords());
        return pageResult;
    }
    
    //批量关闭订单
    @Override
    public void close(Long[] ids, String note) {
        //前端传来的这两个参数不会空
        //将ids拆出来
        for (Long id : ids) {
            LambdaQueryWrapper<OmsOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OmsOrder::getId, id);
            //将参数封装
            OmsOrder one = this.getOne(lambdaQueryWrapper);
            one.setNote(note);
            one.setStatus(4L);
            //更新数据库
            this.update(one, lambdaQueryWrapper);
        }
    }
    
    //批量发货
    @Override
    public void delivery(List<OmsOrderDto> omsOrders) {
        //从前端获取3个关键字段
        for (OmsOrderDto omsOrderDto : omsOrders) {
            omsOrderDto.setId(omsOrderDto.getOrderId());
            //对象拷贝
            OmsOrder omsOrder = new OmsOrder();
            BeanUtils.copyProperties(omsOrderDto, omsOrder);
            //更新三个字段
            this.updateById(omsOrder);
        }
    }
    
    //修改订单费用信息
    @Override
    public void moneyInfo(OmsOrderDto omsOrderDto) {
        omsOrderDto.setId(omsOrderDto.getOrderId());
        //对象拷贝
        OmsOrder omsOrder = new OmsOrder();
        BeanUtils.copyProperties(omsOrderDto, omsOrder);
        
        //更新4个字段
        this.updateById(omsOrder);
    }
    
    //备注订单
    @Override
    public void note(String id, String note, String status) {
        //创建一个对象
        OmsOrder omsOrder1 = new OmsOrder();
        //把map的数据格式转换long并塞入对象
        omsOrder1.setId(Convert.toLong(id));
        omsOrder1.setNote(note);
        omsOrder1.setStatus(Convert.toLong(status));
        //更新数据库
        this.updateById(omsOrder1);
    }
    
    //修改收货人信息
    @Override
    public void receiverInfo(OmsOrderDto omsOrderDto) {
        omsOrderDto.setId(omsOrderDto.getOrderId());
        //对象拷贝
        OmsOrder omsOrder = new OmsOrder();
        BeanUtils.copyProperties(omsOrderDto, omsOrder);
        
        //更新9个字段
        this.updateById(omsOrder);
    }
    
    @Override
    public OmsOrderDto getByIdd(String id) {
        //根据id查询表订单表OmsOrder
        OmsOrder OmsOrder = this.getById(id);
        
        //对象拷贝进主OmsOrderDto
        OmsOrderDto omsOrderDto = new OmsOrderDto();
        BeanUtils.copyProperties(OmsOrder, omsOrderDto);
        
        //查询订单操作历史记录OmsOrderOperateHistory表
        LambdaQueryWrapper<OmsOrderOperateHistory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OmsOrderOperateHistory::getOrderId, id);
        List<OmsOrderOperateHistory> omsOrderOperateHistories = omsOrderOperateHistoryMapper.selectList(lambdaQueryWrapper);
        omsOrderDto.setHistoryList(omsOrderOperateHistories);
        //查询订单中所包含的商品OmsOrderItem表
        LambdaQueryWrapper<OmsOrderItem> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(OmsOrderItem::getOrderId, id);
        List<OmsOrderItem> omsOrderItems = omsOrderItemMapper.selectList(lambdaQueryWrapper2);
        omsOrderDto.setOrderItemList(omsOrderItems);
        //返回
        return omsOrderDto;
    }
    
    //批量删除订单
    @Override
    public boolean deletes(Long[] ids) {
        
        List<Long> lists = (List<Long>) Convert.toList(ids);
        List<OmsOrder> omsOrders = this.listByIds(lists);
        boolean b = false;
        for (OmsOrder omsOrder : omsOrders) {
            Long status = omsOrder.getStatus();
            //如果查到的订单未关闭,那么就返回false
            
            if (!(status==0L||status==1L||status==2L)){
                this.removeByIds(lists);
                b=true;
            }else {
                b=false;
            }
        }
        return b;
    }
}
